<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>地域</title>
    <script src="/js/vue.js"></script>
    <link rel="stylesheet" href="/css/index.css">
    <script src="/js/index.js"></script>
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <template>
        <!--    搜索    -->
        <el-row>
            <!--一行分为4列-->
            <el-col :span="3">
                <el-input
                        size="mini"
                        placeholder="请输入菜单名称"
                        v-model="searchForm.name">
                </el-input>
            </el-col>
            </el-col>
            <el-col :span="2">
                &nbsp;&nbsp;<el-button type="primary" size="mini" icon="el-icon-search" @click="search">搜索</el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="success" size="mini" icon="el-icon-plus" @click="handleAdd">添加</el-button>
            </el-col><el-col :span="2"> <el-button type="primary" size="mini" icon="el-icon-refresh" @click="refresh">刷新</el-button></el-col>
            <el-col :span="12">
                <div></div>
            </el-col>
        </el-row>
        <el-table
                :data="tableData"
                border
                style="width: 100%"
        >
<!--            <el-table-column-->
<!--                    fixed-->
<!--                    prop="code"-->
<!--                    label="ID"-->
<!--                    width="100"-->
<!--                    align="center">-->
<!--            </el-table-column>-->
            <el-table-column
                    prop="name"
                    label="地名"
                    width="100"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="pcode"
                    label="父代码"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="isvalid"
                    label="是否有效"
                    width="100"
                    :formatter="getStatus">
            </el-table-column>
<!--            <el-table-column-->
<!--                    prop="sortno"-->
<!--                    label="排序号"-->
<!--                    width="100">-->
<!--            </el-table-column>-->
            <el-table-column
                    prop="firstletters"
                    label="城市名首字母"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="allletters"
                    label="城市名"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="description"
                    label="描述"
                    width="100">
            </el-table-column>

            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    width="180"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页功能-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[ 5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
        <!--    更新或添加的From表单    -->
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item label="地名" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="父代码" :label-width="formLabelWidth">
                    <el-input v-model="form.pcode" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="是否有效" prop="isvalid">
                    <el-select v-model="form.isvalid" placeholder="请选择是否有效或无效">
                        <el-option label="无效" value="0"></el-option>
                        <el-option label="有效" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序号" :label-width="formLabelWidth">
                    <el-input v-model="form.sortno" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="城市名首字母" :label-width="formLabelWidth" prop="firstletters">
                    <el-input v-model="form.firstletters" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="城市名" :label-width="formLabelWidth" prop="allletters">
                    <el-input v-model="form.allletters" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                    <el-input v-model="form.description" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrUpdate">确 定</el-button>
            </div>
        </el-dialog>
    </template>
</div>


<script>
    // js代码
    var vue = new Vue({
        el: '#app',
        data() {   //存放使用的所有属性数据
            return { //方便编写表单校验
                tableData: [],//表格数据
                currentPage: 1,//第几页
                pageSize: 5,//每页显示数量
                total: 10,//总数量
                searchForm: {name: ''},//封装查询对象
                dialogFormVisible: false,//是否显示对话框
                title: '景点添加',//默认显示
                form: {
                    name:'',
                    pcode:'',
                    isvalid:'',
                    sortno:'',
                    firstletters:'',
                    allletters:'',
                    description:'',
                },//from表单
                rules: {
                    name: [
                        {required: true, message: '请输入地名', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    isvalid: [
                        {required: true, message: '请选择是否有效', trigger: 'change'}
                    ],
                    firstletters: [
                        {required: true, message: '请输入城市名首字母', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    allletters: [
                        {required: true, message: '请输入城市名', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                },
                formLabelWidth: '120px',//label的宽度
            }
        },
        created: function () { //页面初始化时，需要执行的方法
            //调用加载数据方法
            this.loadData();
        },
        methods: { //存放所有使用的方法（函数）
            //封装请求数据的方法
            loadData() {
                //定义_this = this
                var _this = this
                //axios向后台发请求
                axios.post('/tDicArea/queryPage',//请求的url
                    {
                        "pageNo": this.currentPage,
                        "pageSize": this.pageSize,
                        "data": {"name": this.searchForm.name}
                    }//请求的参数 json格式
                )
                    .then(function (response) {//回调函数
                        //前端控制台打印  response 是 JSON对象
                        console.log(response);
                        console.log(JSON.stringify(response));
                        //response获取后端数据是一定要加data 相当于从相应的body中获取
                        if (response.data.code == 200) {
                            //这里面直接写this县东港与axios对象 不是vue
                            //this.tableData = response.data.data.list;  X错误的
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {//异常处理
                        console.log(error);
                    });
            },
            //获取性别状态 把状态数字,转换成字符
            getStatus(row, column, cellValue, index) {
                //cellValue   status的值
                /* var returnValue = '';
                 if(cellValue==1){
                     returnValue= "启用";
                 }else{
                     returnValue= "禁用";
                 }
                 return returnValue;*/
                return cellValue == 0 ? "有效" : "无效";
            },
            //每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                //调用加载数据方法
                this.loadData();
            },

            //页码发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                //调用加载数据方法
                this.loadData();
            }
            ,
            //搜索方法
            search() {
                this.loadData();
            },
            //刷新
            refresh() {
                //原始js重新请求
                location.reload();
            },
            //跳转更新
            handleEdit(row) {
                console.log(row);
                // alert(index+","+JSON.stringify(row))
                this.dialogFormVisible = true;
                //让title变化
                this.title = '部门更新';
                //给表单赋值
                this.form = row;
            }
            ,
            //跳转添加
            handleAdd() {
                //让对话框显示
                this.dialogFormVisible = true;
                this.title = '景区添加';
                this.form = {};
            }
            ,
            //真正的添加或更新
            addOrUpdate() {
                var uri = "/tDicArea/add";
                if (this.title == '部门更新') {
                    uri = "/tDicArea/update";
                }
                var _this = this
                //axios向后台发请求
                axios.post(uri,//请求的url
                    this.form)//请求的参数 json格式
                    .then(function (response) {//回调函数
                        //前端控制台打印  response 是 JSON对象
                        console.log(response);
                        console.log(JSON.stringify(response));
                        //response获取后端数据是一定要加data 相当于从相应的body中获取
                        if (response.data.code == 200) {
                            _this.$message({
                                message: '恭喜你，这是一条成功消息',
                                type: 'success'
                            });
                            _this.dialogFormVisible = false;
                            _this.loadData();
                        } else {
                            _this.$message.error('错了哦，这是一条错误消息');
                        }
                    })
                    .catch(function (error) {//异常处理
                        console.log(error);
                    });
            }
            ,
            //处理删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    var _this = this
                    axios.delete('/tDicArea/delete/' + row.code)
                        .then(function (response) {
                            if (response.data.code == 200) {
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                _this.dialogFormVisible = false;
                                _this.loadData();
                            } else {
                                _this.$message("操作失败")
                            }
                        });
                }).catch(function (error) {

                });
            },  //用户性别 把状态数字,转换成字符
            getStatus(row, column, cellValue, index) {
                return cellValue == 1 ? "有效" : "无效";
            }
        }
    });
</script>
</body>
</html>